#! /usr/bin/env python

import time

class Turing():
	
	prog = []
	nastro = {}
	npos = 0
	ppos = 0
	stato = 0
	nodo = 0
	stop = False
	
	def reset(self):	
		self.nastro = {}
		self.npos = 0
		self.ppos = 0
		self.stato = 0
		self.nodo = 0
		self.stop = False
		
	def set_raw_prog(self,raw):
		for l in raw.split("\n"):
			self.prog.append(l.split(" "))
	
	def set_raw_nastro(self,raw):
		x = 0
		for c in raw:
			self.nastro[x] = c
			x = x + 1
	
	def getnastro(self,pos):
		try:
			return self.nastro[pos]
		except:
			self.nastro[pos] = "-"
			return "-"
	
	def next(self):
		if self.ppos == len(self.prog):
			self.ppos = 0
		if int(self.stato) == int(self.prog[self.ppos][0]):
			if self.getnastro(self.npos) == self.prog[self.ppos][1]:
				self.stato = self.prog[self.ppos][2]
				self.nastro[self.npos] = self.prog[self.ppos][3]
				if self.prog[self.ppos][4] == ">":
					self.npos = self.npos + 1
				if self.prog[self.ppos][4] == "<":
					self.npos = self.npos - 1
				self.nodo = 0
		self.nodo = self.nodo + 1
		self.ppos = self.ppos + 1
		if self.nodo == len(self.prog)+1:
			self.stop = True

